//
//  ApiTest.m
//  EFExtend
//
//  Created by mac on 15/6/29.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "ApiTest.h"
#import "ApiTestPresent.h"


@interface ApiTest ()<PresenterCallBackProtocol>
@property (nonatomic,strong) ApiTestPresent *apiPresent;
@end

@implementation ApiTest

- (void)dealloc
{
   
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    self.apiPresent = [[ApiTestPresent alloc]initWithInPlist:@"inputParams.plist"];
    self.apiPresent.delegate = self;
    
    [self.tableView reloadData];
}

- (HMDialogue *)upload:(NSData*)data to:(NSString*)command alias:(NSString*)alias{
    HMDialogue *dia = [[[[[[self webApiWithCommand:command timeout:20.f] setWebName:alias] setWebMethod:@"POST"] setWebHttpMock:[HMWebAPI methodMockSuccess]] setWebUrlEncoding:NSUTF8StringEncoding] setWebData:data];
    //    HMDialogue *dia = [[[[[self webApiWithCommand:command timeout:20.f] setWebName:alias] setWebMethod:@"UPLOAD"] setWebHttpMock:[HMWebAPI methodMockSuccess]] setWebUploadBlock:^(id<AFMultipartFormData> formData) {
    //        [formData appendPartWithFormData:data name:@"data"];
    //    }];
    [dia send];
    return  dia;
}


- (NSDictionary*)testApiInPlist:(NSString*)inputPlist forApp:(NSString*)app interface:(NSString*)inf{
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[HMSandbox pathWithbundleName:nil fileName:inputPlist]];
    dic = [dic objectForKey:app];
    return [dic valueForKey:inf];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            
        }
    }
}


#pragma  mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *data = [self.apiPresent.apiList valueForKey:[self.apiPresent.apiList.allKeys safeObjectAtIndex:section]];

    return [[data objectForKey:@"commands"] allKeys].count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.apiPresent.apiList.allKeys.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    
    return [self.apiPresent.apiList.allKeys safeObjectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"listCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    
    NSDictionary *data = [self.apiPresent.apiList valueForKey:[self.apiPresent.apiList.allKeys safeObjectAtIndex:indexPath.section]];
    
    cell.textLabel.text = [[[[data objectForKey:@"commands"] allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2];
    }] safeObjectAtIndex:indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *appName = [self.apiPresent.apiList.allKeys safeObjectAtIndex:indexPath.section];
     NSDictionary *data = [self.apiPresent.apiList valueForKey:[self.apiPresent.apiList.allKeys safeObjectAtIndex:indexPath.section]];
    NSString *command = [[[[data objectForKey:@"commands"] allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
       return [obj1 compare:obj2];
    }] safeObjectAtIndex:indexPath.row];
    
    [self.apiPresent doSomething:appName attributes:@[@{[ApiTestPresent AppName]:appName,[ApiTestPresent Command]:command}]];
    
}


- (void)presenter:(Presenter*)presenter doSome:(NSString *)something bean:(id)bean state:(PresenterProcess)state error:(NSError *)error{

    INFO(something,bean);
    HMUIAlertView *alert = nil;
    
    Class alertZ = [HMUIAlert7View class];
    if (IOS9_OR_LATER) {
        alertZ = [HMUIAlertView class];
    }
    alert = [alertZ alertWithTitle:something message:[bean description]];
    [alert addButtonWithTitle:@"确定"];
    [alert show];
}

@end
